/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.ActCep;
import br.com.foxline.model.CharterSDI;
import br.com.foxline.util.Msg;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class ActCepService extends GenericService<ActCep> {

    public ActCepService() {
        super(ActCep.class);
    }

    public void saveActCep(ActCep actCep) {
        try {
            if (checkActCepDescription(actCep)) {
                save(actCep);
                Msg.messagemInfo("Ato Cep Cadastrado com Sucesso");
            } else {
                Msg.messagemError("O campo Descrição é obrigatório");
            }
        } catch (Exception erro) {
        }
    }

    public void updateActCep(ActCep actCep) {
        try {

            update(actCep);
            Msg.messagemInfo("Ato Cep Atualizado com Sucesso");

        } catch (Exception erro) {
        }

    }

    /**
     * Auto complemento para tipo ato cep
     *
     * @param autocomplentoatocep
     * @return
     */
    public List<ActCep> autoComplete(String str) {
        String sql = "Select ac from ActCep ac where lower(ac.description) like lower(:desc)";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("desc", str + "%");
        return (List<ActCep>) query.getResultList();
    }

    /**
     * verifica se o campo descrição está vazio
     *
     * @param verificacampo
     * @return
     */
    public boolean checkActCepDescription(ActCep actCep) {
        boolean b;
        if (!actCep.getDescription().equals("")) {
            b = true;
        } else {
            b = false;
        }
        return b;
    }

    public List<ActCep> findActCep(ActCep actCep) {
        String sql = "Select a from ActCep a Where ";

        if ((!actCep.getCod().equals("")) && (actCep.getCod() != null)) {
            sql += "  lower(a.cod) like lower(:cod)  and ";
        }

        if ((!actCep.getDescription().equals("")) && (actCep.getDescription() != null)) {
            sql += "  lower(a.description) like lower(:des)  and ";
        }

        sql += " 1=1 ";

        Query query = getEntityManager().createQuery(sql);

        if ((!actCep.getCod().equals("")) && (actCep.getCod() != null)) {
            query.setParameter("cod", "%" + actCep.getCod() + "%");
        }
        if ((!actCep.getDescription().equals("")) && (actCep.getDescription() != null)) {
            query.setParameter("des", "%" + actCep.getDescription() + "%");
        }
        return query.getResultList();

    }
    
    public ActCep findActCod(String Cod) {
        ActCep act; 
        String sql = "Select a from ActCep a Where  a.cod like lower(:cod) ";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("cod", Cod);
        try {
            act = (ActCep) query.getSingleResult();
        } catch (NoResultException nr) {
            return null;
        } catch (NonUniqueResultException nre) {
            return null;
        }
        return act;
    }
    
     public ActCep existActCepDescription(ActCep td) {
        ActCep actCep = null;
        String sql = "Select a from ActCep a where a.description like :d";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("d", td.getDescription());
       
        try {
            actCep = (ActCep) query.getResultList().get(0);
        } catch (NoResultException erro) {           
        } catch (NonUniqueResultException nre) {
            return null;
        } catch (Exception e) {
            return null;
        }
        return actCep;
    }
}
